package com.ztjava.article.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Page;
import com.ztjava.article.model.ArticleCatalog;
import com.ztjava.article.model.ArticleComment;
import com.ztjava.article.model.ArticleContent;
import com.ztjava.article.service.ArticleService;
import com.ztjava.article.validator.AddCommentValidator;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.client.member.MobileClient;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;

@ControllerClass(client=MobileClient.class,path="/article")
@UserController(portal=Constant.P_MEMBER)
public class ArticleController extends BaseController{
	private static ArticleService articleService = Duang.duang(ArticleService.class);
	
	public void index(){
		String custom = getPara(0);
		if(custom!=null&&!custom.equals("")&&!custom.equals("index")){
			ArticleContent articleContent = ArticleContent.dao.findFirst("select * from "+ArticleContent.tablename+" where custom=?",custom);
			if(articleContent!=null&&articleContent.getStr("enable_flag").equals("T")){
				if(articleContent.get("redirect")!=null&&!articleContent.getStr("redirect").equals("")){
					redirect(articleContent.getStr("redirect"));
					return;
				}
				ArticleCatalog articleCatalog = ArticleCatalog.dao.findById(articleContent.getInt("catalog_id"));
				if(articleCatalog==null||articleCatalog.getStr("enable_flag").equals("F")){
					renderError(404);
					return;
				}
				articleContent.set("click", articleContent.getInt("click")+1).update();
				setAttr("articleContent", articleContent);
				setAttr("articleCatalog", articleCatalog);
				render(articleCatalog.getStr("show_tpl"));
				return;
			}
		}
		
		render("index.html");
	}
	
	public void catalog(){
		Integer id = getParaToInt("id",0);
		ArticleCatalog articleCatalog = ArticleCatalog.dao.findById(id);
		if(articleCatalog==null||articleCatalog.getStr("enable_flag").equals("F")){
			renderError(404);
			return;
		}
		setAttr("articleCatalog", articleCatalog);
		List<ArticleCatalog> childList = ArticleCatalog.dao.find("select * from "+ArticleCatalog.tablename+" where parent_id=? and enable_flag='T' order by sort asc,id asc",id);
		setAttr("childList", childList);
		render(articleCatalog.getStr("catalog_tpl"));
	}
	@UserMethod(option="ARTICLE_AUTH")
	public void list(){
		Integer id = getParaToInt("id",0);
		ArticleCatalog articleCatalog = null;
		if(id==0){
			String custom = getPara(0, "");
			articleCatalog = ArticleCatalog.dao.findFirst("select * from "+ArticleCatalog.tablename+" where custom=? and enable_flag='T' order by id asc",custom);
			if(articleCatalog==null){
				renderError(404);
				return;
			}
		}else {
			articleCatalog = ArticleCatalog.dao.findById(id);
			if(articleCatalog==null||articleCatalog.getStr("enable_flag").equals("F")){
				renderError(404);
				return;
			}
		}
		setAttr("articleCatalog", articleCatalog);
		List<ArticleCatalog> subList = ArticleCatalog.dao.find("select * from "+ArticleCatalog.tablename+" where parent_id=? order by sort asc,id asc",articleCatalog.getInt("id"));
		setAttr("subList", subList);
		render(articleCatalog.getStr("list_tpl"));
	}
	//异步加载信息
	public void ajaxLoadContent(){
		String where = "";
		Integer catalog_id = getParaToInt("catalog_id");
		Map<String,String[]> map = new HashMap<String, String[]>(getParaMap());
		if(catalog_id>0){
			ArticleCatalog articleCatalog = ArticleCatalog.dao.findById(catalog_id);
			where = where + "and B.num like '"+articleCatalog.getStr("num")+"%'";
			map.put("num", new String[]{articleCatalog.getStr("num")});
		}
		
		Page<ArticleContent> pd = articleService.contentPage(client, map);
		setAttr("infoList", pd.getList());
		setAttr(Constant.RESULT, Constant.SUCCESS);
		renderJson();
	}
	@UserMethod(option="ARTICLE_AUTH")
	public void show(){
		Integer id = getParaToInt("id",0);
		ArticleContent articleContent = ArticleContent.dao.findById(id);
		if(articleContent==null||articleContent.getStr("enable_flag").equals("F")){
			renderError(404);
			return;
		}
		if(articleContent.get("redirect")!=null&&!articleContent.getStr("redirect").equals("")){
			redirect(articleContent.getStr("redirect"));
			return;
		}
		ArticleCatalog articleCatalog = ArticleCatalog.dao.findById(articleContent.getInt("catalog_id"));
		if(articleCatalog==null||articleCatalog.getStr("enable_flag").equals("F")){
			renderError(404);
			return;
		}
		articleContent.set("click", articleContent.getInt("click")+1).update();
		//前端加载评论
		if(articleContent.getStr("is_comment").equals("T")){
			Page<ArticleComment> commentPd = articleService.getCommentPage(client, 
					getParaMapCustom(new String[]{"enable_flag","T"},
					new String[]{"content_id",id.toString()},
					new String[]{client.getSortOrder(),"asc"},
					new String[]{client.getPageSize(),"40"}));
			setAttr("commentPd", commentPd);
		}
		setAttr("articleContent", articleContent);
		setAttr("articleCatalog", articleCatalog);
		render(articleCatalog.getStr("show_tpl"));
	}
	
	@UserMethod
	@Before(AddCommentValidator.class)
	public void doAddComment(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		Integer content_id = getParaToInt("content_id");
		Integer reply_id = getParaToInt("reply_id",0);
		ArticleComment articleComment = new ArticleComment();
		articleComment.set("content_id", content_id);
		articleComment.set("content", getPara("content"));
		articleComment.set("create_user", authSession.getId());
		if(reply_id>0){
			ArticleComment reply = ArticleComment.dao.findById(reply_id);
			if(reply!=null){
				articleComment.set("reply_id", reply.getInt("id"));
				articleComment.set("reply_user", reply.getInt("create_user"));
				articleComment.set("reply_content", reply.getStr("content"));
			}
		}
		articleComment.save();
		articleService.updateContentComment(content_id);
		renderSuccess();
	}
}
